package com.tutorial.bf.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

/**
 * BeanFactory后处理器-BeanFactoryPostProcessor
 *
 * @ClassName BeanFactoryPostProcessor
 * @Description BeanFactory后处理器-BeanFactoryPostProcessor
 * @Author G
 * @Date 2025/3/11 14:29
 */
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("执行了MyBeanFactoryPostProcessor的postProcessBeanFactory方法");
        // 修改某个BeanDefinition
        // BeanDefinition beanDefinition = beanFactory.getBeanDefinition("userService");
        // beanDefinition.setBeanClassName("com.tutorial.bf.dao.impl.UserDaoImpl");
        // 创建BeanDefinition
        BeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClassName("com.tutorial.bf.dao.impl.PersonDaoImpl");
        // 强转成DefaultListableBeanFactory
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
        defaultListableBeanFactory.registerBeanDefinition("personDao", beanDefinition);
    }
}
